package cc.comac.mom.datahandlers.entity;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cc.comac.mom.bean.EquipRepairWorkHour;
import cc.comac.mom.bean.base.LimitPageBean;
import cc.comac.mom.dao.entity.EquipRepairWorkHourDao;
import cc.comac.mom.entity.EquipRepairWorkHourEntity;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.ResultBean;

@Component
@Transactional
public class EquipRepairWorkHourDataHandler extends ServiceImpl<EquipRepairWorkHourDao, EquipRepairWorkHourEntity> {
	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;
	@Autowired
	private EquipRepairWorkHourDao equipRepairWorkHourDao;
	
	//保存
	public ResultBean insert(EquipRepairWorkHour obj) {
		ResultBean resultBean = new ResultBean(true);
		if(obj == null || obj.getInnerData() == null) {
			throw new RuntimeException("数据为空");
		}
		if(StringUtils.isEmpty(obj.getInnerData().getUser())) {
			throw new RuntimeException("人员为空");
		}
		baseEntityDataUtil.setBaseEntityData(obj.getInnerData());
		obj.getInnerData().setUser(baseEntityDataUtil.getCurrentUser());
		obj.getInnerData().setUserJobNumber(baseEntityDataUtil.getJobNumber());
		int insert = equipRepairWorkHourDao.insert(obj.getInnerData());
		if(insert<1) {
			throw new RuntimeException("保存人员工时失败");
		}
		return resultBean;
	}
	
	//修改
	public ResultBean update(EquipRepairWorkHour obj) {
		ResultBean resultBean = new ResultBean(true);
		if(obj == null || obj.getInnerData() == null) {
			throw new RuntimeException("数据为空");
		}
		if(StringUtils.isEmpty(obj.getInnerData().getUser())) {
			throw new RuntimeException("人员为空");
		}
		baseEntityDataUtil.updateBaseEntityData(obj.getInnerData());
		int insert = equipRepairWorkHourDao.updateById(obj.getInnerData());
		if(insert<1) {
			throw new RuntimeException("保存人员工时失败");
		}
		return resultBean;
	}
	
	//删除人员工时
	public ResultBean delete(EquipRepairWorkHour obj) {
		ResultBean resultBean = new ResultBean(true);
		if(obj == null || obj.getInnerData() == null) {
			throw new RuntimeException("数据为空");
		}
		EquipRepairWorkHourEntity selectById = equipRepairWorkHourDao.selectById(obj.getInnerData().getId());
		if(selectById == null) {
			throw new RuntimeException("该信息不存在");
		}
		baseEntityDataUtil.deleteBaseEntityData(selectById);
		int updateById = equipRepairWorkHourDao.updateById(selectById);
		if(updateById<1) {
			throw new RuntimeException("删除人员工时失败");
		}
		return resultBean;
	}
	//查询	
	public LimitPageBean<EquipRepairWorkHourEntity> selectByCondition(EquipRepairWorkHour obj) {
    	QueryWrapper<EquipRepairWorkHourEntity> queryWrapper = new QueryWrapper<EquipRepairWorkHourEntity>();
    	queryWrapper.eq("row_status",1);
    	if(obj.getPageSize() == 0) {
    		obj.setPageSize(10);
    	}
		if(obj != null && obj.getInnerData() != null) {		
    		if(obj.getInnerData().getUser() != null && !"".equals(obj.getInnerData().getUser())) {
    			queryWrapper.like("user",obj.getInnerData().getUser());
    		}
    		if(obj.getInnerData().getRepairId()!= 0) {
    			queryWrapper.eq("repair_id", obj.getInnerData().getRepairId());
    		}
		}
		Page<EquipRepairWorkHourEntity> page = new Page<>(obj.getPageNum(),obj.getPageSize(),true);
		IPage<EquipRepairWorkHourEntity> iPage = equipRepairWorkHourDao.selectPage(page, queryWrapper);
		@SuppressWarnings("unchecked")
		LimitPageBean<EquipRepairWorkHourEntity> limitPageBean = (LimitPageBean<EquipRepairWorkHourEntity>) LimitPageBean.of(iPage);
		return limitPageBean;
		
	}
}
